﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class UsersController : Controller
    {   
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// lists all users
        /// </summary>
        /// <returns>json users array</returns>
        public ActionResult GetUsers()
        {
            UserContainer container = UserModels.GetUserArray();
            return Json(container, JsonRequestBehavior.AllowGet);            
        }

        /// <summary>
        /// select user by id (used to get user info for populate edit form when user is selected)
        /// </summary>
        /// <returns>json array with one record</returns>
        public ActionResult GetUsersById()
        {
            decimal idUser = 0;
            Decimal.TryParse(Request.Params["Id"], out idUser);

            UserContainer container = UserModels.GetUserArrayById(idUser);

            return Json(container, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// change password for current logged user
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePass()
        {
            UserModels userInfo = Session["user"] as UserModels;
            decimal id = userInfo.Id;
            string password = Request.Params["pass"];

            if (password != null)
            {
                UserModels.ChangePassword(password,id);
            }

            return Json(new { success = true, message = "some message" });
        }

        /// <summary>
        /// edit user info
        /// </summary>
        /// <returns></returns>
        public ActionResult EditUser()
        {
            decimal idUser = 0;
            Decimal.TryParse(Request.Params["Id"], out idUser);

            UserModels user = new UserModels();

            decimal available = 0;
            Decimal.TryParse(Request.Params["Available"], out available);

            user.Available = available;
            user.LastName = Request.Params["LastName"];
            user.FirstName = Request.Params["FirstName"];
            user.Email = Request.Params["Email"];
            user.Username = Request.Params["Username"];
            user.AccessLevel = Request.Params["AccessLevel"];
            user.Id = idUser;

            string nullPass = Request.Params["Password"];
            if (nullPass == "")
            {
                user.Password = null;
            }
            else
            {
                user.Password = Request.Params["Password"];
            }

            UserModels userCheck = new UserModels();
            userCheck = UserModels.CheckUsername(user);

            string messageBack = null;

            try
            {
                if (idUser == 0)
                {
                    if (userCheck == null)
                    {
                        UserModels.AddUser(user);
                    }
                    else
                    {
                        messageBack = "Username-ul exista deja";
                    }
                }
                else
                {
                    if (idUser != 0)
                    {
                        if (userCheck == null)
                        {
                            if (user.Password == null)
                            {
                                UserModels.EditUser(user);
                            }
                            else
                            {
                                UserModels.EditUserPass(user);
                            }
                        }
                        else
                        {
                            messageBack = "Username-ul exista deja";
                        }
                    }
                }
                return Json(new { success = true, message = messageBack });
            }
			catch (Exception ex)
			{
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume!";
				}
				return Json(new { success = true, description = messageBack });
			}         
        }

    }
}
